﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Buccaneer
{
    public partial class Addressbar : UserControl
    {
        #region Constructor

        public Addressbar()
        {
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);

            InitializeComponent();

            g = this.CreateGraphics();

            tbx_addressInput.Font = Options.Font;

            if (Separator == null) LoadImages();
        }

        #endregion

        #region Events

        private void tbx_addressInput_KeyDown(object sender, KeyEventArgs e)
        {
            //*/
            if (e.KeyData == Keys.Enter)
            {
                if (tbx_addressInput.Text != "")
                {
                    addText(tbx_addressInput.Text.Replace(Environment.NewLine, "\\"));
                }
                try { Navigating.Invoke(this, EventArgs.Empty); }
                catch (NullReferenceException) { }
                this.Invalidate(true);
                this.Update();
                Console.WriteLine("enter handeled");
            }
            else if (e.KeyData == Keys.Back && tbx_addressInput.Text == "" && textParts.Length > 0)
            {
                try { tbx_addressInput.Text = textLabels[textLabels.Length - 1].Text + "x"; }
                catch (Exception) { }
                //*/
                deleteText();
                tbx_addressInput.Select(tbx_addressInput.Text.Length, 0);
                //*/
            }
            //*/
            this.Refresh();
        }

        private void tbx_addressInput_TextChanged(object sender, EventArgs e)
        {
            //*/
            if (tbx_addressInput.Text.Length > pathSeparator.Length && tbx_addressInput.Text.Substring(tbx_addressInput.Text.Length - pathSeparator.Length, pathSeparator.Length) == pathSeparator)
            {
                addText(tbx_addressInput.Text.Substring(0, tbx_addressInput.Text.Length - 1));
            }
            tbx_addressInput.Size = new Size((int)(g.MeasureString(tbx_addressInput.Text, tbx_addressInput.Font).Width) + 30, 20);
            //*/
        }

        private void Addressbar_MouseClick(object sender, MouseEventArgs e)
        {
            //*/
            this.Focus();
            if (separators != null && separators.Length > 0)
            {
                for (int i = separators.Length - 1; i >= 0; i--)
                {
                    if (e.X > separators[i].Location.X) break;
                    else
                    {
                        
                    }
                }
            }
            //*/
        }

        #endregion

        #region private attributes

        private BitmapButton[] separators;
        private Label[] textLabels;

        private int selectedItem = 0;
        private string pathSeparator = @"\";
        private string[] textParts = new string[0];
        private Graphics g;
        private Point cursorPos;
        private bool drawCursorFlag = true;

        #endregion

        #region OnPaint

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            drawText(e);
            //drawCursor();
        }

        #endregion

        #region private functions

        private void addText(string text)
        {
            Array.Resize<string>(ref textParts, textParts.Length + 1);
            textParts[textParts.Length - 1] = text;
            tbx_addressInput.Text = "";
            tbx_addressInput.Location = new Point((int)g.MeasureString(this.Text, Options.Font).Width + (Separator[0].Width + 10) * this.textParts.Length + 10, 10);
        }

        private void deleteText()
        {
            tbx_addressInput.Location = new Point((int)g.MeasureString(this.Text, Options.Font).Width + (Separator[0].Width + 10) * this.textParts.Length + 10, 10);
            if (textParts.Length > 0)
            {
                tbx_addressInput.Text = textParts[textParts.Length - 1];
                Array.Resize<string>(ref textParts, textParts.Length - 1);
            }
            else tbx_addressInput.Text = "";
        }

        private void drawText(PaintEventArgs e)
        {
            Point pos = new Point(10, 10);

            if (textParts.Length == 0) return;

            foreach (string text in textParts)
            {
                e.Graphics.DrawString(
                    text,
                    Options.Font,
                    Brushes.Black,
                    pos
                    );
                pos.X += (int)e.Graphics.MeasureString(text, Options.Font).Width + 5;
                pos.Y -= 8;

                e.Graphics.DrawImage(
                    Separator[0],
                    pos
                    );
                pos.X += Separator[0].Width + 5;
                pos.Y += 8;
            }
        }

        private void drawCursor()
        {
            if (drawCursorFlag)
                g.DrawLine(
                    Pens.Black,
                    cursorPos,
                    new Point(cursorPos.X, cursorPos.Y + (int)Options.Font.GetHeight())
                    );
        }

        #endregion

        #region public attributes

        public static Bitmap[] Separator;

        public event EventHandler Navigating;

        public override string Text
        {
            get
            {
                string retval = "";
                for (int i = 0; i < textParts.Length; i++)
                {
                    retval += textParts[i] + pathSeparator;
                }
                return retval;
            }
            set
            {
                textParts = new string[0];
                string[] s = value.Split(new string[] { pathSeparator }, StringSplitOptions.None);
                tbx_addressInput.Text = s[s.Length - 1];
                for (int i = 0; i < s.Length - 1; i++)
                    addText(s[i]);
            }
        }

        public string PathSeparator
        {
            get { return pathSeparator; }
            set { pathSeparator = value; }
        }

        public static void LoadImages()
        {
            Separator = new Bitmap[6];
            // Load all images from Layout Dir
            Separator[(int)ButtonState.Normal] = new Bitmap(Environment.CurrentDirectory + Options.LayoutFolder + "addressbarSeparator.png");
            try
            {
                Separator[(int)ButtonState.Hovered] = new Bitmap(Environment.CurrentDirectory + Options.LayoutFolder + "addressbarSeparatorHover.png");
                Separator[(int)ButtonState.Clicked] = new Bitmap(Environment.CurrentDirectory + Options.LayoutFolder + "addressbarSeparatorClick.png");
                Separator[(int)ButtonState.Focussed] = new Bitmap(Environment.CurrentDirectory + Options.LayoutFolder + "addressbarSeparatorFocus.png");
                Separator[(int)ButtonState.Disabled] = new Bitmap(Environment.CurrentDirectory + Options.LayoutFolder + "addressbarSeparatorDisabled.png");
                Separator[(int)ButtonState.Error] = new Bitmap(Environment.CurrentDirectory + Options.LayoutFolder + "addressbarSeparatorError.png");
            }
            catch (Exception) { }
        }

        #endregion

        #region public functions



        #endregion
    }

}